package com.walle.concurrenttest.waitnotify;

/**
 * @author zhangjiapeng
 * @Package com.walle.concurrenttest.waitnotify
 * @Description: ${todo}
 * @date 2019/2/18 10:03
 */
public class OddPrint implements Runnable {



    private OddEvenPrint oddEvenPrint;

    public OddPrint(OddEvenPrint oddEvenPrint) {
        this.oddEvenPrint = oddEvenPrint;
    }

    @Override
    public void run() {

        while (true){
            synchronized (oddEvenPrint){
                if(oddEvenPrint.getNum() % 2 != 0){
                    oddEvenPrint.print();
                    oddEvenPrint.notifyAll();
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else {
                    try {
                        oddEvenPrint.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        }
    }
}
